package cn.com.swan.framework.global;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.dispatcher.RequestMap;

import cn.com.swan.framework.model.BaseModel;
import cn.com.swan.framework.module.operation.model.Operation;
import cn.com.swan.framework.module.user.model.User;

import com.opensymphony.xwork2.ActionContext;

public class SwanGlobalFilter implements Filter {

	public void destroy() {
		// TODO Auto-generated method stub

	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		
		
		Object ctx = WebContextUtil.getServletContext().getAttribute(Constant.WEB_KEY_CTX);
		HttpServletRequest httpServletRequest = (HttpServletRequest) request;
		HttpServletResponse httpServletResponse = (HttpServletResponse) response;
		if (ctx == null) {
			WebContextUtil.getServletContext().setAttribute(Constant.WEB_KEY_CTX,httpServletRequest.getContextPath());
		}
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String notLoginPaths = WebContextUtil.getServletContext().getInitParameter("notLoginPath");
		if (notLoginPaths != null) {
			String requestPath = httpServletRequest.getRequestURI();
			if (notLoginPaths.indexOf(requestPath.substring(WebContextUtil.CTX.length(),requestPath.length() )) != -1) {
				chain.doFilter(request, response);
				return;
			}else if(requestPath.endsWith(".jsp")){
				httpServletResponse.sendRedirect(WebContextUtil.CTX+"/common/jsp/notPermission.jsp");
			}
		}

		chain.doFilter(request, response);
	}

	public void init(FilterConfig filterConfig) throws ServletException {
		String encode = filterConfig.getInitParameter("encoding");
	}

}
